#include "word_prob.h"

#include <cstdio>
#include <boost/lexical_cast.hpp>

#include "base/logging.h"

using namespace std;

double WordProb::GetWordProb(const string & word) const {
  // cout << cache_.size() << endl;
  map<string, double>::const_iterator it = cache_.find(word);
  if (it != cache_.end()) {
    return it->second;
  }
  return 0.5;
}

bool WordProb::Load() {
  string file("./words.prob_df");
  FILE * data = fopen(file.c_str(), "r");
  if (!data) {
    LOG_WARN("open data file " << file << " error");
    return false;
  }
  LOG_DEBUG("open file " << file << " ok");

  static const int kMaxLine = 512;
  char line[kMaxLine];
  line[kMaxLine - 1] = 0;

  while(fgets(line, kMaxLine - 1, data)) {
    char * p = line;
    while(*p && (*p != ' ')) {
      ++p;
    }
    *p = '\0';
    ++p;
    char * q = p;
    while(*q && *q != '\r' && *q != '\n') {
      ++q;
    }
    *q = '\0';
    cache_.insert(make_pair(string(p), boost::lexical_cast<double>(line)));
  }
  
  return true;
}

